//package com.sxjh.aspect;
//
//
//import com.alibaba.fastjson.JSON;
//import lombok.SneakyThrows;
//import lombok.extern.slf4j.Slf4j;
//import org.slf4j.MDC;
//import org.springframework.beans.factory.BeanFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.web.servlet.HandlerExecutionChain;
//import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
//
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletRequestWrapper;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpServletResponseWrapper;
//import java.io.*;
//import java.util.UUID;
//
///**
// * @Description:
// * @Param:
// * @return:
// * @Author:
// * @Date: 2023/09/04
// */
//
//@WebFilter(filterName = "globalLogFilter", urlPatterns = "/*")
//@Slf4j
//@Configuration
//public class GlobalLogFilter implements Filter {
//    @Autowired
//    private BeanFactory beanFactory;
//
//    @SneakyThrows
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) {
//        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
//        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
//        MDC.put("TRACE_ID", UUID.randomUUID().toString().replace("-", ""));
//        // 打印请求相关参数
//        log.info("======= Start =========");
//        log.info("URL            : {}", httpServletRequest.getRequestURL().toString());
//        log.info("IP             : {}", httpServletRequest.getRemoteAddr());
//        log.info("HTTP Method    : {}", httpServletRequest.getMethod());
//        log.info("HTTP param     : {}", httpServletRequest.getQueryString());
//
//        //转换成代理类
//        ResponseWrapper wrapperResponse = new ResponseWrapper(httpServletResponse);
//        RequestWrapper requestWrapper = new RequestWrapper(httpServletRequest);
//
//        long before = System.currentTimeMillis();
//
//        //通过ApplicationContext上下文（spring）找到RequestMappingHandlerMapping这个bean
//        RequestMappingHandlerMapping handlerMapping = beanFactory.getBean(RequestMappingHandlerMapping.class);
//        //RequestMappingHandlerMapping是对应url和处理类方法的一个类
//        HandlerExecutionChain handlerChain = handlerMapping.getHandler(requestWrapper);
//        //通过处理链找到对应的HandlerMethod类
////        HandlerMethod handler = (HandlerMethod) handlerChain.getHandler();
////        //HandlerMethod中有bean和method
////        Object bean = handler.getBean();//处理请求的类
////        Method method = handler.getMethod();//处理请求的方法
//        // 打印调用 controller 的全路径以及执行方法
////        log.info("Class Method   : {}.{}", bean, method);
//
//        log.info("HTTP body      : {}", JSON.toJSON(requestWrapper.getBody()));
//        filterChain.doFilter(requestWrapper, wrapperResponse);
//        //获取返回值
//        byte[] content = wrapperResponse.getContent();
//        // 打印出参
//        log.info("Response Args  : {}", JSON.toJSON(new String(content)));
//        // 执行耗时
//        log.info("Time-Consuming : {} ms", System.currentTimeMillis() - before);
//        log.info("========== End ============");
//        MDC.clear();
//        //把返回值输出到客户端
//        ServletOutputStream out = httpServletResponse.getOutputStream();
//        out.write(content);
//        out.flush();
//    }
//}
//
//class ResponseWrapper extends HttpServletResponseWrapper {
//
//    private ByteArrayOutputStream buffer;
//
//    private ServletOutputStream out;
//
//    public ResponseWrapper(HttpServletResponse httpServletResponse) {
//        super(httpServletResponse);
//        buffer = new ByteArrayOutputStream();
//        out = new WrapperOutputStream(buffer);
//    }
//
//    @Override
//    public ServletOutputStream getOutputStream() throws IOException {
//        return out;
//    }
//
//    @Override
//    public void flushBuffer() throws IOException {
//        if (out != null) {
//            out.flush();
//        }
//    }
//
//    public byte[] getContent() throws IOException {
//        flushBuffer();
//        return buffer.toByteArray();
//    }
//
//    class WrapperOutputStream extends ServletOutputStream {
//        private ByteArrayOutputStream bos;
//
//        public WrapperOutputStream(ByteArrayOutputStream bos) {
//            this.bos = bos;
//        }
//
//        @Override
//        public void write(int b) throws IOException {
//            bos.write(b);
//        }
//
//        @Override
//        public boolean isReady() {
//            return false;
//        }
//
//        @Override
//        public void setWriteListener(WriteListener arg0) {
//
//        }
//    }
//
//}
//
//class RequestWrapper extends HttpServletRequestWrapper {
//    private final String body;
//
//    public RequestWrapper(HttpServletRequest request) {
//        super(request);
//        StringBuilder stringBuilder = new StringBuilder();
//        BufferedReader bufferedReader = null;
//        InputStream inputStream = null;
//        try {
//            inputStream = request.getInputStream();
//            if (inputStream != null) {
//                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
//                char[] charBuffer = new char[128];
//                int bytesRead = -1;
//                while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
//                    stringBuilder.append(charBuffer, 0, bytesRead);
//                }
//            } else {
//                stringBuilder.append("");
//            }
//        } catch (IOException ex) {
//
//        } finally {
//            if (inputStream != null) {
//                try {
//                    inputStream.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//            if (bufferedReader != null) {
//                try {
//                    bufferedReader.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//        body = stringBuilder.toString();
//    }
//
//    @Override
//    public ServletInputStream getInputStream() throws IOException {
//        final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
//        ServletInputStream servletInputStream = new ServletInputStream() {
//            @Override
//            public boolean isFinished() {
//                return false;
//            }
//
//            @Override
//            public boolean isReady() {
//                return false;
//            }
//
//            @Override
//            public void setReadListener(ReadListener readListener) {
//            }
//
//            @Override
//            public int read() throws IOException {
//                return byteArrayInputStream.read();
//            }
//        };
//        return servletInputStream;
//
//    }
//
//    @Override
//    public BufferedReader getReader() throws IOException {
//        return new BufferedReader(new InputStreamReader(this.getInputStream()));
//    }
//
//    public String getBody() {
//        return this.body;
//    }
//
//}